package org.skydiveapp2;

import java.util.List;

import org.skydiveapplibrary.data.Signature;
import org.skydiveapplibrary.repository.SignatureRepository;
import org.skydiveapplibrary.ui.DrawingView;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class SignatureActivity extends Activity
{
	// repositories
	private SignatureRepository repository = new SignatureRepository(this);
	
	@Override 
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    	
    	setContentView(R.layout.signature_view);
    	
    	// set subtitle text
    	setSubtitleText(R.string.signature_header_label);
    	
    	// setup autocomplete license field
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, repository.getSignatureLicenses());
        AutoCompleteTextView signatureLicenseField = (AutoCompleteTextView) findViewById(R.id.SignatureLicenseField);
        signatureLicenseField.setAdapter(adapter);
        
		overridePendingTransition(R.anim.fadein,
				R.anim.hold);
    }
	
	private void save()
	{
		// get ui components
		DrawingView drawingView = (DrawingView) findViewById(
				R.id.SignatureDrawingView);
		TextView licenseField = (TextView) findViewById(
				R.id.SignatureLicenseField);

		// get signature data
		byte[] data = drawingView.getImageData();
		String license = licenseField.getText().toString();

		// get log entry ids
		List<Integer> logEntryIds = getIntent().getIntegerArrayListExtra(
				Constants.EXTRA_SELECTED_LOG_ENTRY_IDS);

		// create signature
		Signature signature = new Signature();
		signature.setImage(data);
		signature.setLicense(license);

		// save signature
		SignatureRepository repository = new SignatureRepository(this);
		repository.addSignature(signature, logEntryIds);

		// finish
		close();
	}
	
	private void clear()
	{
		DrawingView drawingView = (DrawingView)  findViewById(R.id.SignatureDrawingView);
		drawingView.clear();
	}
	
	private void setSubtitleText(int id)
	{
		// set subtitle label
		TextView subtitleTextView = (TextView) this.getActionBar().getCustomView().findViewById(R.id.actionbarSubtitle);
		subtitleTextView.setText(id);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_save, menu);
    	inflater.inflate(R.menu.menu_clear, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_save:
    		save();
            return true;
            
        case R.id.menu_back:
			close();
            return true;
            
        case R.id.menu_clear:
        	clear();
            return true;
            
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
    	overridePendingTransition(R.anim.hold,
                R.anim.fadeout);
	}
}
